package cn.iocoder.yudao.adminserver.modules.live.service.project.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.adminserver.modules.live.controller.project.vo.*;
import cn.iocoder.yudao.adminserver.modules.live.controller.projectPlan.vo.LiveProjectPlanCreateReqVO;
import cn.iocoder.yudao.adminserver.modules.live.controller.projectPlan.vo.LiveProjectPlanRespVO;
import cn.iocoder.yudao.adminserver.modules.live.convert.project.LiveProjectConvert;
import cn.iocoder.yudao.adminserver.modules.live.convert.projectPlan.LiveProjectPlanConvert;
import cn.iocoder.yudao.adminserver.modules.live.dal.dataobject.project.LiveProjectDO;
import cn.iocoder.yudao.adminserver.modules.live.dal.dataobject.projectPlan.LiveProjectPlanDO;
import cn.iocoder.yudao.adminserver.modules.live.dal.mysql.project.LiveProjectMapper;
import cn.iocoder.yudao.adminserver.modules.live.dal.mysql.projectPlan.LiveProjectPlanMapper;
import cn.iocoder.yudao.adminserver.modules.live.service.project.LiveProjectService;
import cn.iocoder.yudao.adminserver.modules.system.controller.user.vo.user.SysUserSimpleRespVO;
import cn.iocoder.yudao.adminserver.modules.system.service.user.SysUserService;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.adminserver.modules.tool.enums.ToolErrorCodeConstants.PROJECT_NOT_EXISTS;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 项目 Service 实现类
 *
 * @author 芋艿
 */
@Service
@Validated
public class LiveProjectServiceImpl implements LiveProjectService {

    @Resource
    private SysUserService userService;

    @Resource
    private LiveProjectMapper projectMapper;

    @Resource
    private LiveProjectPlanMapper projectPlanMapper;

    @Override
    public Long createProject(LiveProjectCreateReqVO createReqVO) {
        // 插入
        LiveProjectDO project = LiveProjectConvert.INSTANCE.convert(createReqVO);
        projectMapper.insert(project);
        Long id = project.getId();
        List<LiveProjectPlanCreateReqVO> liveProjectPlanCreateReqVOList = createReqVO.getLiveProjectPlanCreateReqVOList();
        liveProjectPlanCreateReqVOList.stream().forEach(createReqvo ->
                projectPlanMapper.insert(LiveProjectPlanConvert.INSTANCE.convert(createReqvo))
        );
        return id;
    }

    @Override
    public void updateProject(LiveProjectUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateProjectExists(updateReqVO.getId());
        // 更新
        LiveProjectDO updateObj = LiveProjectConvert.INSTANCE.convert(updateReqVO);
        projectMapper.updateById(updateObj);
    }

    @Override
    public void deleteProject(Long id) {
        // 校验存在
        this.validateProjectExists(id);
        // 删除
        projectMapper.deleteById(id);
    }

    private void validateProjectExists(Long id) {
        if (projectMapper.selectById(id) == null) {
            throw exception(PROJECT_NOT_EXISTS);
        }
    }

    @Override
    public LiveProjectDO getProject(Long id) {
        return projectMapper.selectById(id);
    }

    @Override
    public List<LiveProjectDO> getProjectList(Collection<Long> ids) {
        return projectMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<LiveProjectDO> getProjectPage(LiveProjectPageReqVO pageReqVO) {
        return projectMapper.selectPage(pageReqVO);
    }

    @Override
    public List<LiveProjectDO> getProjectList(LiveProjectExportReqVO exportReqVO) {
        return projectMapper.selectList(exportReqVO);
    }

    @Override
    public List<LiveProjectAndPlanRespVO> getSimpleProject(Date date, String projectName) {
        List<SysUserSimpleRespVO> simpleDepts = userService.getSimpleDepts();
        Set<Long> userIds = CollectionUtils.convertSet(simpleDepts, SysUserSimpleRespVO::getId);

        DateTime beginDateTime = DateUtil.beginOfYear(date);
        DateTime endDateTime = DateUtil.endOfYear(date);

        LiveProjectBaseVO liveProjectBaseVO = new LiveProjectBaseVO();
        liveProjectBaseVO.setProjectStartDate(beginDateTime);
        liveProjectBaseVO.setProjectEndDate(endDateTime);
        liveProjectBaseVO.setProjectName(projectName);
        List<LiveProjectDO> liveProjectDOS = projectMapper.selectList(liveProjectBaseVO, userIds);

        Set<Long> projectIds = CollectionUtils.convertSet(liveProjectDOS, LiveProjectDO::getId);
        List<LiveProjectPlanDO> liveProjectPlanDOS = projectPlanMapper.selectList(projectIds);

        List<LiveProjectAndPlanRespVO> liveProjectAndPlanRespVOS = LiveProjectConvert.INSTANCE.convertList03(liveProjectDOS);

        for (int i = 0; i < liveProjectAndPlanRespVOS.size(); i++) {
            LiveProjectAndPlanRespVO liveProjectAndPlanRespVO = liveProjectAndPlanRespVOS.get(i);
            List<LiveProjectPlanDO> collect = liveProjectPlanDOS.stream().filter(el -> liveProjectAndPlanRespVO.getId().equals(el.getProjectId())).collect(Collectors.toList());
            List<LiveProjectPlanRespVO> liveProjectPlanRespVOS = LiveProjectPlanConvert.INSTANCE.convertList(collect);
            liveProjectAndPlanRespVO.setLiveProjectPlanRespVOList(liveProjectPlanRespVOS);
        }

        return liveProjectAndPlanRespVOS;
    }

}
